package com.example.tool.adapter;

import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.tool.R;
import com.example.tool.bean.UserEntity;
import com.example.tool.listener.OnItemListener;
import com.example.tool.util.CalendarUtil;

import static com.example.tool.util.CalendarUtil.DATE_FORMAT_YMD;


/**
 * @author : liangkun
 * Date : 2020/7/11
 * Describe : 家庭成员管理
 */
public class HealthMemberAdapter extends BaseQuickAdapter<UserEntity, BaseViewHolder> {

    private OnItemListener onItemListener;

    public HealthMemberAdapter() {
        super(R.layout.health_user_item_member_info);
    }


    @Override
    protected void convert(@NonNull BaseViewHolder helper, UserEntity userEntity) {
//        //判断显示添加view
        if (checkIsAddView(userEntity)) {
            //显示成员id
            helper.setVisible(R.id.user_layout_info, true);
            helper.setVisible(R.id.add_user_layout, false);
            if (TextUtils.isEmpty(userEntity.getName())) {
                helper.setText(R.id.user_tv_name, "未完善");
            } else {
                helper.setText(R.id.user_tv_name, userEntity.getName());
            }
            helper.setText(R.id.user_tv_sex, userEntity.getSexString());
            helper.setText(R.id.user_tv_age, CalendarUtil.getCurrentDate(DATE_FORMAT_YMD));
            helper.setText(R.id.user_tv_phone, getMaskText(userEntity.getPhone(), 3, 3));
            if (TextUtils.isEmpty(userEntity.getCertificate())) {
                helper.setText(R.id.user_tv_id, "未实名");
            } else {
                helper.setText(R.id.user_tv_id, getMaskText(userEntity.getCertificate(), 3, 0));
            }
//            helper.setText(R.id.user_tv_relation, userEntity.getRelationship());

            helper.setImageResource(R.id.user_iv_role, getUserIcon(userEntity.getRelationId(), userEntity.getSex()));
        } else {
            //显示add的view
            helper.setVisible(R.id.user_layout_info, false);
            helper.setVisible(R.id.add_user_layout, true);
        }
        helper.getView(R.id.add_user_layout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemListener!=null){
                    onItemListener.OnItemClick(helper.getAdapterPosition());
                }
            }
        });

    }
    public void setOnAddClickLister(OnItemListener onAddClickLister){
        onItemListener = onAddClickLister;
    }
    /**
     * @param userEntity
     * @return
     * @description 判断是否显示添加的view
     * @author liangkun
     * @time 2020/7/11 11:30
     */
    private boolean checkIsAddView(UserEntity userEntity) {

        return !TextUtils.isEmpty(userEntity.getPhone())
                && !TextUtils.isEmpty(userEntity.getRelationship());
    }

    public static String getMaskText(String str, int startLength, int endLength) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        int length = str.length();
        if (startLength + endLength >= length) {
            return str;
        }
        StringBuilder builder = new StringBuilder();
        builder.append(str.substring(0, startLength));
        for (int i = startLength; i < length - endLength; i++) {
            builder.append("*");
        }
        builder.append(str.substring(length - endLength));

        return builder.toString();
    }

    @DrawableRes
    private int getUserIcon(int relationId, int sex) {
        switch (relationId) {
            case 17:
                //本人
            case 18:
                //配偶
                if (sex == 1) {
                    return R.drawable.user_iv_role_male2;
                } else if (sex == 2) {
                    return R.drawable.user_iv_role_female2;
                } else {
                    return R.drawable.user_iv_role_default;
                }
            case 19:
                //子女
                if (sex == 1) {
                    return R.drawable.user_iv_role_male1;
                } else if (sex == 2) {
                    return R.drawable.user_iv_role_female1;
                } else {
                    return R.drawable.user_iv_role_default;
                }
            case 20:
                //父母
                if (sex == 1) {
                    return R.drawable.user_iv_role_male3;
                } else if (sex == 2) {
                    return R.drawable.user_iv_role_female3;
                } else {
                    return R.drawable.user_iv_role_default;
                }
            default:
                return R.drawable.user_iv_role_default;
        }
    }


}
